#pragma once

#include <common.h>

template<class K, class V>
class StackMap {
public:
  StackMap() = default;
  ~StackMap() = default;

  void new_stack();
  void delete_last_stack();
  void push(K key, V val);
  bool find(K key);
  V get(K key);

private:
  std::vector<std::map<K, V>> map_vector;
};

template<class K, class V>
void StackMap<K, V>::new_stack() {
  this->map_vector.push_back(std::map<K, V>());
}

template<class K, class V>
void StackMap<K, V>::delete_last_stack() {
  Assert(this->map_vector.empty() == false, "stack map is empty");
  this->map_vector.pop_back();
}

template<class K, class V>
void StackMap<K, V>::push(K key, V val) {
  Assert(this->map_vector.empty() == false, "stack map is empty");
  this->map_vector.back().insert({key, val});
}

template<class K, class V>
bool StackMap<K, V>::find(K key) {
  for(const std::map<K, V> & map : this->map_vector) {
    if(map.find(key) != map.end()) return true;
  }
  return false;
}

template<class K, class V>
V StackMap<K, V>::get(K key) {
  for(std::map<K, V> map : this->map_vector) {
    if(map.find(key) != map.end()) return map[key];
  }
  Panic("can't find key");
  return 0;
}
